package com.example.childrendapp.login;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

import com.example.childrendapp.Main;
import com.example.childrendapp.R;
import com.example.childrendapp.database.DataBaseHelper;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class Login extends Activity implements OnClickListener {
    private static final int TEST = 1;
	private static final String TAG = "Login";
	private DataBaseHelper mDB;
    private TextView mError;
    private TextView mLinkRegister;
    private EditText mName, mPass;
    private Button mLogin;
    private SQLiteDatabase myDataBase;
    private String name,pass;
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login_xml);
            mDB = new DataBaseHelper(this);
            mError=(TextView) findViewById(R.id.txtError);
            mLinkRegister=(TextView) findViewById(R.id.txtLink_Register);
            mName = (EditText) findViewById(R.id.txtUser);
            mPass = (EditText) findViewById(R.id.txtPass);
            mLogin = (Button) findViewById(R.id.btnLogin);
            mLogin.setOnClickListener(this);
            mLinkRegister.setOnClickListener(this);
            name=mName.getText().toString();
    		pass=mPass.getText().toString();
            
           try {
                    mDB.createDataBase();
            } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
            mDB.openDataBase();
            try {
                    mDB.copyDataBase();
            } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
            myDataBase = mDB.getWritableDatabase();
                     
            
    }
    @Override
    protected void onResume() {
    	// TODO Auto-generated methmCod stub
    	super.onResume();
    	Log.i(TAG,"onResume");
    }

	public void onClick(View v) {

		if(v==mLogin){
			Cursor mCursor = myDataBase.rawQuery("SELECT * FROM USER WHERE UserName='name' and PassWord='pass'", null);
            if(mCursor!=null){
            	Intent data = new Intent(this,Main.class);
				data.putExtra("VALUE1", name);
				data.putExtra("VALUE2", pass);
				startActivity(data);        	   	
                      	
            }
            
            else{
            	mError.setText("Invalid, Enter Again");
				mName.setText("");
				
				mPass.setText("");
				mName.requestFocus();
            }
		}
        
		else if(v==mLinkRegister){
			
			Intent intent=new Intent(this,Register.class);
			this.startActivityForResult(intent, TEST);
			
			
		}
			
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if(resultCode==RESULT_OK){
			name=data.getExtras().getString("NAME");
			pass=data.getExtras().getString("PASS");
			mName.setText(name);
			mPass.setText(pass);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	/*public boolean check(){
		for (int j = 0; j <5; j++) {
			if (ArrName[j].toString().trim().equals(name) && ArrPass[j].toString().trim().equals(pass) ) {
				
					return true;	
					}
			
		}
		return false;
	}*/
	@Override
	protected void onPause() {
		super.onPause();
		Log.i(TAG,"onPause");
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.i(TAG,"onStop");
	}
}

		
	

		
		
	

		



